﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.03.22
 * 描 述：项目管理
 */
var selectedRow;
var refreshGirdData;
var bootstrap = function ($, learun) {
    "use strict";
    var page = {
        init: function () {
            page.initGrid();
            page.bind();
        },
        bind: function () {
            // 搜索框初始化
            $('#txt_Keyword').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    var keyword = $this.val();
                    page.search({keyword: keyword});
                }
            });
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({keyword: keyword});
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 新增
            $('#lr_add').on('click', function () {
                // top.learun.frameTab.open({
                //     id: top.learun.newGuid(),
                //     css: 'fa fa-project',
                //     name: '新增项目',
                //     path: config.webportal_server + '/pages/workflow/wfmyTask/custmerWorkFlowForm.html?shcemeCode=bill_project_apply&tabIframeId=875da9e0eafc4ee5901af6100f1bc473&type=0&access_token=' + config.getToken().access_token,
                // });
                selectedRow = null;
                learun.layerForm({
                    id: 'form',
                    title: '新增项目',
                    url: config.webportal_server + 'pages/pbd/project/form.html?access_token=' + config.getToken().access_token,
                    width: 1080,
                    height: 740,
                    callBack: function (id) {
                        return top[id].acceptClick(refreshGirdData);
                    }
                });
            });
            // 编辑
            $('#lr_edit').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                selectedRow = $('#gridtable').jfGridGet('rowdata');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'form',
                        title: '编辑项目',
                        url: config.webportal_server + 'pages/pbd/project/form.html?access_token=' + config.getToken().access_token,
                        width: 1080,
                        height: 740,
                        callBack: function (id) {
                            return top[id].acceptClick(refreshGirdData);
                        }
                    });
                }
            });
            // 删除
            $('#lr_delete').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                selectedRow = $('#gridtable').jfGridGet('rowdata');
                if (learun.checkrow(keyValue)) {
                    if (selectedRow != null && selectedRow.authStatus == '1') {
                        learun.alert.warning("此项目已提审，不允许删除");
                        return;
                    }
                    learun.layerConfirm('该动作执行后此项将彻底被删除，是否确认删除该项?', function (res) {
                        if (res) {
                            learun.deleteForm(config.base_server() + 'projectapplys/delete/' + keyValue + '?access_token=' + config.getToken().access_token, {keyValue: keyValue}, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });
            // 从excel导入
            $('#lr_import').on('click', function () {
                learun.layerForm({
                    id: 'UploadifyForm',
                    title: '上传文件',
                    url: config.webportal_server + 'pages/pbd/budget/import/uploadifyForm.html?access_token=' + config.getToken().access_token,
                    width: 600,
                    height: 400,
                    callBack: function (id) {
                        return top[id].acceptClick(function (datas) {
                            if (datas) {
                                refreshGirdData();
                            }
                        });
                    }
                });
            });
            // 送审
            $('#lr_send').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                selectedRow = $('#gridtable').jfGridGet('rowdata');
                if (learun.checkrow(keyValue)) {
                    if (selectedRow != null && selectedRow.authStatus == '1') {
                        learun.alert.warning("此项目已送审，不允许重复送审");
                        return;
                    }
                    var isNew = '1';
                    if (selectedRow.authStatus != null) {
                        isNew = '0';
                    }
                    var req = {
                        isNew: isNew,
                        processId: keyValue,
                        schemeCode: 'bill_project_apply',
                        processName: '项目立项申请',
                        processLevel: '0',
                        description: '',
                        auditors: {},
                        formData: JSON.stringify(selectedRow)
                    };
                    learun.layerConfirm('是否确认送审该项目?', function (res) {
                        if (res) {
                            learun.postForm3(config.base_server() + 'wfengines/create?access_token=' + config.getToken().access_token, req, function (res) {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });
            // 取消送审
            $('#lr_cancel').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                selectedRow = $('#gridtable').jfGridGet('rowdata');
                if (learun.checkrow(keyValue)) {
                    if (selectedRow != null && selectedRow.status == '2') {
                        learun.alert.warning("此项目未送审，不至此取消送审");
                        return;
                    }
                    learun.layerConfirm('是否确认取消送审该项目?', function (res) {
                        if (res) {
                            learun.postForm3(config.base_server() + 'wftasks/revoke/' + keyValue + '?access_token=' + config.getToken().access_token, {processId: keyValue}, function (res) {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });
            // 数据导出
            $('#lr_export').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    location.href = config.base_server() + 'projectapplys/exportExcel?processId=' + keyValue + '&access_token=' + config.getToken().access_token;
                }
            });
        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: config.base_server() + 'projectapplys/list?access_token=' + config.getToken().access_token,
                headData: [
                    {label: '项目编码', name: 'code', width: 120, align: 'center'},
                    {label: '项目名称', name: 'name', width: 200, align: 'center'},
                    {label: '项目类别', name: 'category', width: 150, align: 'center'},
                    {label: '项目性质', name: 'nature', width: 150, align: 'center'},
                    {
                        label: '项目立项预算(元)', name: 'totalMoney', width: 180, align: 'center',
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        }
                    },
                    {label: '项目属性', name: 'attribute', width: 150, align: 'center'},
                    {label: '立项部门', name: 'departmentName', width: 150, align: 'center'},
                    {label: '项目负责人', name: 'chargePerson', width: 100, align: 'center'},
                    {label: '项目执行年度', name: 'actionYear', width: 100, align: 'center'},
                    {
                        label: '审批状态', name: 'authStatus', width: 100, align: 'center',
                        formatter: function (cellvalue, row) {
                            if (cellvalue == "") {
                                return "<span class=\"label label-default\">待送审</span>";
                            }
                            if (row.isFinished == 0) {
                                if (cellvalue == 1) {
                                    return "<span class=\"label label-success\">审批中</span>";
                                } else if (cellvalue == -1) {
                                    return "<span class=\"label label-default\">待送审</span>";
                                }
                            } else if (row.hasReject == 2) {
                                return "<span class=\"label label-danger\">拒绝</span>";
                            } else {
                                return "<span class=\"label label-warning\">审批完成</span>";
                            }
                        }
                    },
                    {label: '创建人', name: 'createUsername', width: 100, align: 'center'},
                    {
                        label: "创建时间", name: "createTime", width: 140, align: "center",
                        formatter: function (value) {
                            return learun.formatDate(value, 'yyyy-MM-dd hh:mm:ss');
                        }
                    },
                    {label: 'id', name: 'id', width: 100, align: 'left', hidden: true}
                ],
                dblclick: function (row) {
                    var keyValue = $('#gridtable').jfGridValue('id');
                    selectedRow = row;
                    if (learun.checkrow(keyValue)) {
                        learun.layerForm({
                            id: 'form',
                            title: '编辑项目',
                            url: config.webportal_server + 'pages/pbd/project/form.html?access_token=' + config.getToken().access_token,
                            width: 1080,
                            height: 740,
                            callBack: function (id) {
                                return top[id].acceptClick(refreshGirdData);
                            }
                        });
                    }
                },
                isPage: true,
                reloadSelected: true,
                mainId: 'id'
            });

            page.search();
        },
        search: function (param) {
            param = param || {};
            $('#gridtable').jfGridSet('reload', param);
        }
    };

    refreshGirdData = function () {
        page.search();
    };

    page.init();
}


